$(document).ready(function() {
    var siteUrl = $('#siteUrl').val();


    $('#error-sign-up').modal();
    $('#error-sign-in').modal();
    $('#success-sign-up').modal();
    $('#show-pass').click(function() {
	$('#wrap-edit-pass').slideToggle();
	$(this).find('i').toggleClass('icon-arrow-down icon-arrow-up');
    });
    $(document).on('click', '.flirt-user', function() {
	var receiver = $(this).attr('receiver');
	var isPostFacebook = $(this).attr('is-post-facebook');
	var senderName = $(this).attr('senderName');
	var receiverName = $(this).attr('receiverName');
	if (isPostFacebook == null) {
	    $.post(siteUrl + 'flirt/send', {receiver: receiver}, function(result) {
	    });
	} else {
	    $.post(siteUrl + 'flirt/send', {receiver: receiver, isPostFacebook: isPostFacebook, senderName: senderName, receiverName: receiverName}, function(result) {
	    });
	}
	$(this).remove();
	$('#modal-flirt').modal();

	return false;
    });
    $(document).on('click', '.show-box-msg', function() {
	var idBox = $(this).attr('href');
	$(idBox).modal();
	return false;
    });

    /*****************************************************************
     * when click chat with
     */
    $(document).on("click", '.chat-with', function(e) {
	var idUser = $(this).attr('id-user');
	var username = $(this).attr('user-name');

	//check if box chat was exist, system would show this box again
	var checkBoxExist = $('#wrap-all-box-chat').find('#box-of-user' + idUser);

	if (checkBoxExist.prop('tagName') == null) {

	    //check if user is online
	    $.post(siteUrl + 'chat/checkonline', {idUser: idUser}, function(response) {


		if (response == 'true') {
		    $('#wrap-all-box-chat').append('<div class="box-chat"><div class="name-chat alert alert-info"><img class="status-online" src="' + siteUrl + 'www/img/icon-online.png"/>Chat with ' + username + '<i class="icon-remove-sign remove-box-chat" title="Close"></i></div><div class="content-chat" id="box-of-user' + idUser + '"><ul></ul></div><div class="compose-chat"><textarea placeholder="Press enter to send messages" class="txt-chat" id-user="' + idUser + '" user-name="' + username + '"></textarea></div></div>');
		} else {
		    $('#wrap-all-box-chat').append('<div class="box-chat"><div class="name-chat alert alert-info"><img class="status-online" src="' + siteUrl + 'www/img/icon-offline.png"/>Chat with ' + username + '<i class="icon-remove-sign remove-box-chat" title="Close"></i></div><div class="content-chat" id="box-of-user' + idUser + '"><ul></ul></div><div class="compose-chat"><textarea placeholder="Press enter to send messages" class="txt-chat" id-user="' + idUser + '" user-name="' + username + '"></textarea></div></div>');
		}

		//after 10s get news message
		var refreshChatBox = setInterval(function() {

		    //check if there is any message
		    var timeLastMsg = $('#box-of-user' + idUser + ' ul').find('li').last().attr('time-created');
		    if (timeLastMsg != null) {
			//get more message
			$.post(siteUrl + 'chat/getmsg', {timeLastMsg: timeLastMsg, idUser: idUser}, function(result) {
			    $('#box-of-user' + idUser + ' ul').append(result);

			});
		    }
		    /*
		     * 
		     * check if user is online
		     */
		    $.post(siteUrl + 'chat/checkonline', {idUser: idUser}, function(response) {
			if (response == 'true') {
			    $('#box-of-user' + idUser).parent().find('img').attr('src', siteUrl + 'www/img/icon-online.png');
			} else {
			    $('#box-of-user' + idUser).parent().find('img').attr('src', siteUrl + 'www/img/icon-offline.png');
			}
		    });
		}, 5000);

		/*
		 * Close chat box
		 */
		$('#box-of-user' + idUser).parent().find('.remove-box-chat').click(function() {

		    //remove chat box
		    $('#box-of-user' + idUser).parent().remove();
		    //clear interval for this chat box
		    clearInterval(refreshChatBox);
		});

	    });
	}
	return false;
    });
    /*
     * when user type to chat
     */
    $(document).on("keyup", '.txt-chat', function(e) {

	var objTxtChat = $(this);
	var idUser = $(this).attr('id-user');
	var content = $(this).val();
	var userName = $(this).attr('user-name');
	//var timeLastMsg = $(this).parent().parent().find('li').last().attr('time_created');
	var timeLastMsg = $(this).parent().parent().find('li').last().attr('time-created');

	//press enter
	if (e.keyCode == 13) {
	    //text box has value?
	    if (content != '\n') {
		$(this).val('');
		content = content.replace(/(\r\n|\n|\r)/gm, "");
		$.post(siteUrl + 'chat/send', {idUser: idUser, content: content, userName: userName, timeLastMsg: timeLastMsg}, function(result) {
		    objTxtChat.parent().parent().find('ul').append(result);
		});
	    } else {
		$(this).val('');
	    }
	}
    });


    /***********************************
     * send msg
     */

    $(document).on('submit', '.form-msg', function() {
	var oAnounce = $(this).find('.announce');
	var oThis = $(this);
	$(this).ajaxSubmit({
	    //target: '#form-send-request', //Div tag where content info will be loaded in
	    url: siteUrl + "mailbox/message/new", //The php file that handles the file that is uploaded	
	    beforeSubmit: function() {
		//load progress img when uploading happens
		oAnounce.find('.alert').remove();
		oAnounce.append('<div class="progressbar-send-msg"><img src="' + siteUrl + 'progress_bar.gif"></div>');
	    },
	    success: function(data) {
		//Here code can be included that needs to be performed if Ajax request was successful	    
		oAnounce.find('.progressbar-send-msg').remove();

		var subject = oThis.find('.subject-mg').val();
		var message = oThis.find('.message-mg').val();
		if (subject && message) {
		    oAnounce.append('<div class="alert alert-success"><strong>Congratulation!</strong> You have sent mail successfully</div>');
		} else {
		    oAnounce.append('<div class="alert alert-error"><strong>Error!</strong> Please fill subject and message</div>');
		}
	    }
	});
	return false;
    });

    $('a#advance-search').click(function() {
	$('#box-advance-search').slideToggle();
    });

    $('a.title-tip').click(function() {
	var idBox = $(this).attr('href');
	$(idBox).slideToggle();
    });

});
